package com.csp.base.controller;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.csp.base.model.JobAndTrigger;
import com.csp.base.model.JobRunHistory;
import com.csp.base.service.IJobAndTriggerService;
import com.csp.base.service.JobRunHistoryService;
import com.github.pagehelper.PageInfo;

@RestController
@RequestMapping(value = "/job")
public class JobController {
	
	@Autowired
	private IJobAndTriggerService iJobAndTriggerService;
	
	@Autowired
	private JobRunHistoryService jobRunHistoryService;

	/**
	 * 添加定时任务
	 * @param jobName
	 * @param jobClassName
	 * @param jobGroupName
	 * @param cronExpression
	 * @throws Exception
	 */
	@PostMapping(value = "/addjob")
	public ResponseEntity<Map<String,Object>> addjob( @RequestParam(value = "jobName") String jobName,
						@RequestParam(value = "jobClassName") String jobClassName,
						@RequestParam(value = "jobGroupName") String jobGroupName,
						@RequestParam(value = "cronExpression") String cronExpression) throws Exception {
		iJobAndTriggerService.addJob(jobName,jobClassName, jobGroupName, cronExpression);
		return ResponseEntity.ok(new HashMap<>());
	}
	
	/**
	 * 删除定时任务
	 * @param jobClassName
	 * @param jobGroupName
	 * @throws Exception
	 */
	@PostMapping(value = "/deletejob")
	public ResponseEntity<Map<String,Object>> deletejob(@RequestParam(value = "jobClassName") String jobClassName,@RequestParam(value = "jobGroupName") String jobGroupName) throws Exception {
		iJobAndTriggerService.jobdelete(jobClassName, jobGroupName);
		return ResponseEntity.ok(new HashMap<>());
	}
	
	/**
	 * 修改定时任务的cron编码(如果有正在占停会重新启动)
	 * @param jobName
	 * @param jobGroupName
	 * @param cronExpression
	 * @throws Exception
	 */
	@PostMapping(value = "/reschedulejob")
	public ResponseEntity<Map<String,Object>> rescheduleJob(@RequestParam(value = "jobName") String jobName,
			@RequestParam(value = "jobGroupName") String jobGroupName,
			@RequestParam(value = "cronExpression") String cronExpression) throws Exception {
		iJobAndTriggerService.jobReschedule(jobName, jobGroupName, cronExpression);
		return ResponseEntity.ok(new HashMap<>());
	}

	/**
	 * 暂停定时任务
	 * @param jobClassName
	 * @param jobGroupName
	 * @throws Exception
	 */
	@PostMapping(value = "/pausejob")
	public ResponseEntity<Map<String,Object>> pausejob(@RequestParam(value = "jobName") String jobName,@RequestParam(value = "jobGroupName") String jobGroupName) throws Exception {
		iJobAndTriggerService.jobPause(jobName, jobGroupName);
		return ResponseEntity.ok(new HashMap<>());
	}
	
	/**
	 * 恢复定时任务
	 * @param jobClassName
	 * @param jobGroupName
	 * @throws Exception
	 */
	@PostMapping(value = "/resumejob")
	public ResponseEntity<Map<String,Object>> resumejob(@RequestParam(value = "jobName") String jobName,@RequestParam(value = "jobGroupName") String jobGroupName) throws Exception {
		iJobAndTriggerService.jobresume(jobName, jobGroupName);
		return ResponseEntity.ok(new HashMap<>());
	}
	
	/**
	 * 分页查询定时任务
	 * @param pageNum
	 * @param pageSize
	 * @return
	 */
	@GetMapping(value = "/queryjob")
	public ResponseEntity<Map<String,Object>> queryjob(@RequestParam(value = "pageNum") Integer pageNum,@RequestParam(value = "pageSize") Integer pageSize) {
		PageInfo<JobAndTrigger> jobAndTrigger = iJobAndTriggerService.getJobAndTriggerDetails(pageNum, pageSize);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("JobAndTrigger", jobAndTrigger);
		map.put("number", jobAndTrigger.getTotal());
		return ResponseEntity.ok(map);
	}
	
	/**
	 * 分页查询定时任务
	 * @param pageNum
	 * @param pageSize
	 * @return
	 */
	@GetMapping(value = "/history")
	public ResponseEntity<Map<String,Object>> jobRunHist(@RequestParam(value = "pageNum") Integer pageNum,
										@RequestParam(value = "pageSize") Integer pageSize,
										@RequestParam(value = "jobName") String jobName,
										@RequestParam(value = "jobGroupName") String jobGroupName,
										@RequestParam(value = "jobClassName") String jobClassName
										) {
		PageInfo<JobRunHistory> jobRunHitoryList = jobRunHistoryService.getJobRunHitoryList(pageNum, pageSize, jobName, jobGroupName, jobClassName);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("jobRunHitoryList", jobRunHitoryList);
		map.put("number", jobRunHitoryList.getTotal());
		return ResponseEntity.ok(map);
	}
	
}
